#include "ikeaenvform.h"
#include "ui_ikeaenvform.h"

#include <QDebug>
#include "cirbuff.h"

#include "globalvars.h"

IkeaEnvForm::IkeaEnvForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::IkeaEnvForm)
{
    ui->setupUi(this);

    m_protocol.clearProtocoldata();
    m_serial = new SerialThread;
    connect(m_serial, SIGNAL(signalWarnInfo(int)),
            this, SLOT(slotWarnInfo(int)));
    connect(m_serial, SIGNAL(signalModeInfo(int)),
            this, SLOT(slotModeInfo(int)));
    connect(m_serial, SIGNAL(signalBeepStatu(bool)),
            this, SLOT(slotBeepStatu(bool)));
    connect(m_serial, SIGNAL(signalTempValue(int,QString)),
            this, SLOT(slotTempValue(int,QString)));
    connect(m_serial, SIGNAL(signalHumiValue(int,QString)),
            this, SLOT(slotHumiValue(int,QString)));
    connect(m_serial, SIGNAL(signalShowRecvData(QByteArray)),
            this, SLOT(slotShowRecvData(QByteArray)));

//    ui->te_recvText->append(tr("检测到端口列表:"));
//    ui->te_recvText->append((tr("/dev/ttyAMA0")));
    ui->cb_comPort->addItem("/dev/ttyAMA0");


    ui->cb_baudRate->setCurrentIndex(7);//默认115200
    ui->pb_openSerial->setText(tr("打开串口"));
//    ui->pb_send->setEnabled(false);

    on_pb_openSerial_clicked();
}

IkeaEnvForm::~IkeaEnvForm()
{
    delete ui;
}
void IkeaEnvForm::slotShowRecvData(QByteArray ba)
{
//    ui->te_recvText->append(QString::fromLatin1(ba));
}


void IkeaEnvForm::on_pb_openSerial_clicked()
{
    if(ui->pb_openSerial->text()==tr("打开串口"))
    {
        if(m_serial->openSerialPort())
        {
            qDebug() << "openSerialPort";
            m_serial->start();
            ui->cb_comPort->setEnabled(false);
            ui->cb_baudRate->setEnabled(false);
            ui->cb_crcTest->setEnabled(false);
            ui->cb_dataBit->setEnabled(false);
            ui->cb_stopBit->setEnabled(false);
            ui->lb_statuFlag->setStyleSheet("background-color:red");
            ui->pb_openSerial->setText(tr("关闭串口"));
//            ui->pb_send->setEnabled(true);
        }else
        {
        }
    }else
    {
        m_serial->closeSerialPort();

        ui->cb_comPort->setEnabled(true);
        ui->cb_baudRate->setEnabled(true);
        ui->cb_crcTest->setEnabled(true);
        ui->cb_dataBit->setEnabled(true);
        ui->lb_statuFlag->setStyleSheet("background-color:rgb(130,130,130)");
        ui->pb_openSerial->setText(tr("打开串口"));
//        ui->pb_send->setEnabled(false);
    }
}

void IkeaEnvForm::slotWarnInfo(int flag)
{
    switch(flag)
    {
    case CMD_WarnData: GlobalVars::g_warnInfo = "数据正常"; break;
    case CMD_WarnOvrT: GlobalVars::g_warnInfo = "温度过高";  break;
    case CMD_WarnLowT: GlobalVars::g_warnInfo = "温度过低";  break;
    case CMD_WarnOvrH: GlobalVars::g_warnInfo = "湿度过高";  break;
    case CMD_WarnLowH: GlobalVars::g_warnInfo = "湿度过低";  break;
    }

    ui->lb_warn->setText(QString("警告: ") + GlobalVars::g_warnInfo);
}

void IkeaEnvForm::slotModeInfo(int flag)
{
    switch(flag)
    {
    case 0: GlobalVars::g_workMode = "实时模式"; break;
    case 1: GlobalVars::g_workMode = "温度模式"; break;
    case 2: GlobalVars::g_workMode = "湿度模式"; break;
    }

    ui->lb_mode->setText(QString("模式: ") + GlobalVars::g_workMode);
}

void IkeaEnvForm::slotBeepStatu(bool on)
{
//    if(on)
//    {
//        ui->lb_beep->setStyleSheet("background-color:red");
//        ui->pb_beep->setText("Beep OFF");
//    }else
//    {
//        ui->lb_beep->setStyleSheet("background-color:rgb(130,130,130)");
//        ui->pb_beep->setText("Beep ON");
//    }
}

void IkeaEnvForm::slotTempValue(int flag, QString value)
{
    if(flag == CMD_TempReal)
    {
        GlobalVars::g_tempStr = value;
        ui->lcd_tempV->display(value);
    }else if(flag == CMD_TempMaxV)
    {
        GlobalVars::g_tempStrMax = value;
        ui->lcd_tempMaxV->display(value);
    }else if(flag == CMD_TempMinV)
    {
        GlobalVars::g_tempStrMin = value;
        ui->lcd_tempMinV->display(value);
    }
}

void IkeaEnvForm::slotHumiValue(int flag, QString value)
{
    if(flag == CMD_HumiReal)
    {
        GlobalVars::g_humiStr = value;
        ui->lcd_humiV->display(value);
    }else if(flag == CMD_HumiMaxV)
    {
        GlobalVars::g_humiStrMax = value;
        ui->lcd_humiMaxV->display(value);
    }else if(flag == CMD_HumiMinV)
    {
        GlobalVars::g_humiStrMin = value;
        ui->lcd_humiMinV->display(value);
    }
}

void IkeaEnvForm::on_pb_tempMaxVAdd_clicked()
{
    QByteArray array = m_protocol.packetMsg(DEV_Temp, CMD_TempMaxA);
    m_serial->sendDataToSerial(array);
}

void IkeaEnvForm::on_pb_tempMaxVSub_clicked()
{
    QByteArray array = m_protocol.packetMsg(DEV_Temp, CMD_TempMaxS);
    m_serial->sendDataToSerial(array);
}

void IkeaEnvForm::on_pb_tempMinVAdd_clicked()
{
    QByteArray array = m_protocol.packetMsg(DEV_Temp, CMD_TempMinA);
    m_serial->sendDataToSerial(array);
}

void IkeaEnvForm::on_pb_tempMinVSub_clicked()
{
    QByteArray array = m_protocol.packetMsg(DEV_Temp, CMD_TempMinS);
    m_serial->sendDataToSerial(array);
}

void IkeaEnvForm::on_pb_humiMaxVAdd_clicked()
{
    QByteArray array = m_protocol.packetMsg(DEV_Humi, CMD_HumiMaxA);
    m_serial->sendDataToSerial(array);
}

void IkeaEnvForm::on_pb_humiMaxVSub_clicked()
{
    QByteArray array = m_protocol.packetMsg(DEV_Humi, CMD_HumiMaxS);
    m_serial->sendDataToSerial(array);
}

void IkeaEnvForm::on_pb_humiMinVAdd_clicked()
{
    QByteArray array = m_protocol.packetMsg(DEV_Humi, CMD_HumiMinA);
    m_serial->sendDataToSerial(array);
}

void IkeaEnvForm::on_pb_humiMinVSub_clicked()
{
    QByteArray array = m_protocol.packetMsg(DEV_Humi, CMD_HumiMinS);
    m_serial->sendDataToSerial(array);
}

